package com.xh.audit.api.forest.oaself;

import cn.hutool.core.util.StrUtil;
import com.dtflys.forest.callback.AddressSource;
import com.dtflys.forest.http.ForestAddress;
import com.dtflys.forest.http.ForestRequest;
import lombok.ToString;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;

import java.net.URI;

@Configuration
public class OASelfInfo {
    public static String oaSelfUrl;

    @Value("${servers.oaselfurl}")
    public void setOaSelfUrl(String oaSelfUrl) {
        OASelfInfo.oaSelfUrl = oaSelfUrl;
    }


    @ToString
    public static class OASelfAddressSource implements AddressSource {

        @Override
        public ForestAddress getAddress(ForestRequest forestRequest) {
            String oaselfServerUrl = OASelfInfo.oaSelfUrl;
            URI uri = URI.create(oaselfServerUrl);
            int port = uri.getPort();
            String host = uri.getHost();
            String scheme = uri.getScheme();
            String path = uri.getPath();
            if (StrUtil.isBlank(path)) {
                return new ForestAddress(scheme, host, port);
            }
            return new ForestAddress(scheme, host, port, path);
        }

    }

    @ToString
    public static class ResKeys {
        public static final String CODE = "code";
        public static final String DATA = "data";
    }

    @ToString
    public static class ResState {
        /**
         * {"code":"9999","url":"/"}
         * <p>
         * {"code":"000000","mesg":"处理成功","timestamp":"2023-09-26T09:26:35.475Z","data":{"dept":
         */
        public static final String SUCCESS_CODE = "000000";
        public static final String FAILED_CODE = "9999";
    }

}
